package com.imooc.security.admin;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
import org.springframework.http.*;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@SpringBootApplication
@Controller
@EnableZuulProxy
@Slf4j
public class AdminApplication {

    private RestTemplate restTemplate = new RestTemplate();

    @GetMapping("/login")
    public String goLogin(HttpServletRequest request, HttpSession session){
        return "login";
    }

    @GetMapping("/logout")
    public String lougout(HttpServletRequest request, HttpSession session){
        session.invalidate();
        return "login";
    }

    @GetMapping("/oauth/callback")
    public String callback(String code,String state ,HttpServletRequest request, Model model){

        if(StringUtils.isEmpty(code)){
            return "login";
        }
        log.info("state is"+state);
        Credentials credentials = new Credentials();

        String oauthServiceUrl = "http://gateway.yanxiang.com:9070/token/oauth/token";
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        httpHeaders.setBasicAuth("admin","123456");

        MultiValueMap<String,String> params = new LinkedMultiValueMap<String,String>();
        params.add("code",code);
        params.add("grant_type","authorization_code");
        params.add("redirect_uri","http://admin.yanxiang.com:8088/oauth/callback");

        HttpEntity<MultiValueMap<String,String>> entity = new HttpEntity<>(params,httpHeaders);
        ResponseEntity<TokenInfo> response = restTemplate.exchange(oauthServiceUrl, HttpMethod.POST, entity, TokenInfo.class);
        System.out.println(response.getBody().toString());
        TokenInfo tokenInfo = response.getBody();
        if(tokenInfo.getAccess_token() != null){
            model.addAttribute("code",code);
            model.addAttribute("state",state);
            model.addAttribute("token",tokenInfo.getAccess_token());
            request.getSession().setAttribute("token",response.getBody());
        }else{
            return "login";
        }
        return "index";
    }

    @PostMapping("/login")
    public String login(@RequestParam String username, @RequestParam String password, HttpServletRequest request, Model model){
        Credentials credentials = new Credentials();
        credentials.setUsername(username);
        credentials.setPassword(password);

        String oauthServiceUrl = "http://192.168.0.35:9070/token/oauth/token";
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
        httpHeaders.setBasicAuth("admin","123456");

        MultiValueMap<String,String> params = new LinkedMultiValueMap<String,String>();
        params.add("username",credentials.getUsername());
        params.add("password",credentials.getPassword());
        params.add("grant_type","password");
        params.add("scope","read write");

        HttpEntity<MultiValueMap<String,String>> entity = new HttpEntity<>(params,httpHeaders);
        ResponseEntity<TokenInfo> response = restTemplate.exchange(oauthServiceUrl, HttpMethod.POST, entity, TokenInfo.class);
        System.out.println(response.getBody().toString());
        TokenInfo tokenInfo = response.getBody();
        if(tokenInfo.getAccess_token() != null){
            model.addAttribute("username",username);
            model.addAttribute("password",password);
            model.addAttribute("token",tokenInfo.getAccess_token());
            request.getSession().setAttribute("token",response.getBody());
        }else{
            return "login";
        }
        return "index";
    }

    public static void main(String[] args) {
        SpringApplication.run(AdminApplication.class, args);
    }


}
